package com.tanker.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ExceptionHandler;

import com.tanker.common.exception.BusinessException;
import com.tanker.common.utils.ErrorCode;
import com.tanker.common.utils.JsonResult;
import com.tanker.common.utils.jsonbean.out.ResponseObject;

public class BaseController {

	protected String input;
	protected Map<String, Object> errorModel;
	protected HttpServletRequest request;
	
	protected Map<String,Object> getContent(HttpServletRequest request){
		return (Map<String, Object>) request.getAttribute("content");
	}
	
	protected Object resultMsg(int i) {
		if (i == 1)
			return JsonResult.jsonOk();
		else
			return JsonResult.jsonError();
	}
	
	protected Map<String, Object> resultMap(List<?> list) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rows", list);
		return map;
	}

	public String getInput() {
		return input;
	}

	public void setInput(String input) {
		this.input = input;
	}

	public Map<String, Object> getErrorModel() {
		return errorModel;
	}

	public void setErrorModel(Map<String, Object> errorModel) {
		this.errorModel = errorModel;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

    @ExceptionHandler  
    public String exp(HttpServletRequest request, Exception ex) {  
          
        request.setAttribute("ex", ex);  
        if(ex instanceof BusinessException) {  
            return "error-business";  
        }else if(ex instanceof BusinessException) {  
            return "error-parameter";  
        } else {  
            return "error";  
        }  
    } 
    /**
	 * 返回验证信息，输出错误提示
	 * @param result 验证返回的错误信息集合
	 * @return
	 */
	public ResponseObject<?> validate(BindingResult result)
	{
		ResponseObject<?> responseObject = new ResponseObject<>();
		responseObject.setErrorCode(ErrorCode.SUCCESS);
		 List<FieldError> errors = result.getFieldErrors();
         for(FieldError err : errors) {
         	//所有输入错误都使用这个响应码
         	responseObject.setResCode(ErrorCode.INPUT_ERROR.getCode());
         	responseObject.setResMsg(err.getDefaultMessage());//验证框架中获取的响应信息,bean中message的输入值
         	return responseObject;
         }
		return responseObject;
	}

	protected String getReqIpAddr(HttpServletRequest request) {
		String ip = null;
		try {
			ip = request.getHeader("x-forwarded-for");
			if (!StringUtils.hasText(ip) || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("Proxy-Client-IP");
			}
			if (!StringUtils.hasText(ip) || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("WL-Proxy-Client-IP");
			}
			if (!StringUtils.hasText(ip) || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getRemoteAddr();
			}
		} catch (Exception e) {
			//logger.error("getReqIpAddr >> ", e);
		}
		return ip;
	}
}
